/**
 * 
 */
package com.proyecto.core.Utilidades;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jposition.CharacterInvalidException;
import org.jposition.Colors;
import org.jposition.Coordinate;
import org.jposition.CoordinateRangeException;
import org.jposition.Dimension;
import org.jposition.DimensionRangeException;
import org.jposition.MapTypeException;
import org.jposition.Marker;
import org.jposition.MarkersMap;
import org.jposition.NoExistColorException;
import org.jposition.SizeRangeException;
import org.jposition.ZoomRangeException;

import common.Logger;

/**
 * @author VICTOR GARNICA
 *
 */
public class jMapa extends JFrame implements ActionListener{
	private JTextField txtDire;
	private JButton btnBuscar;
	private JLabel lblImagenMapa;
	private JPanel myPanel;
	private Panel panel1,panel2;
	private Double sLalitude = 0d;
	private Double sLonguitude = 0d;
	private Integer sZoom;
	public jMapa(){
		
	}
	public jMapa(Double xLalitude, Double xLonguitude,Integer xZoom){
		sLalitude = xLalitude;
		sLonguitude = xLonguitude;
		sZoom = xZoom;
		initComponents();
		
	}
	public jMapa(Double xLalitude, Double xLonguitude){
		sLalitude = xLalitude;
		sLonguitude = xLonguitude;
		initComponents();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		try {
			//Coordinate coordenada = new Coordinate(txtDire.getText(),"");
			//System.out.println(":::sLalitude-> " + sLalitude);
			//System.out.println(":::sLonguitude-> " + sLonguitude);
			Coordinate coordenada = new Coordinate( sLalitude,sLonguitude);
			//Coordinate coordenada = new Coordinate( 7.919593,-72.522397);
			//7.919593
			//-72.522397
			Marker punto = new Marker(Colors.ColorRed, coordenada, Colors.SizeMid, 'p');
			ArrayList<Marker> listaPuntos = new ArrayList<Marker>();
			listaPuntos.add(punto);
			MarkersMap mapa = new MarkersMap("",coordenada,17,listaPuntos);
			mapa.setDimmension(new Dimension(500, 500));
			mapa.setMapType(MarkersMap.RoadMap);
			//ImageIcon ii = (ImageIcon)mapa.getMapImage();
			//ImageReference imageReference1 = (ImageReference)( ii.getImage());
			//labelImagen.setIcon(imageReference1);
			lblImagenMapa.setSize(500, 500);
			
			lblImagenMapa.setIcon(mapa.getMapImage());
			//bajarImagenUrl.getImageFromUrl(mapa.getMapURL(),sLalitude.toString(),sLonguitude.toString(),"");
			/*
			File Fichero = new File("foto.jpg");
			String formato = "jpg";
			BufferedImage imagen = new BufferedImage(this.getContentPane().getWidth(),this.getContentPane().getHeight(), BufferedImage.TYPE_INT_ARGB);
			Graphics g = imagen.getGraphics();
			this.getContentPane().paint(g);
			ImageIO.write(imagen, formato, Fichero);
			
		    */
			
		} catch (IOException ioe) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (CharacterInvalidException cie) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (ZoomRangeException zre) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (DimensionRangeException dre) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (MapTypeException mte) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (NoExistColorException mte) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (SizeRangeException mte) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (CoordinateRangeException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
	}
	
	public void ejecutarBusqueda(){
		try {
			Coordinate coordenada = new Coordinate( sLalitude,sLonguitude);
			Marker punto = new Marker(Colors.ColorRed, coordenada, Colors.SizeMid, 'p');
			ArrayList<Marker> listaPuntos = new ArrayList<Marker>();
			listaPuntos.add(punto);
			MarkersMap mapa = new MarkersMap("",coordenada,sZoom,listaPuntos);
			mapa.setDimmension(new Dimension(500, 500));
			mapa.setMapType(MarkersMap.RoadMap);
			lblImagenMapa.setSize(500, 500);
			lblImagenMapa.setIcon(mapa.getMapImage());
	        
			File Fichero = new File("src/htdocs/imagenes/foto.jpg");
			String formato = "jpg";
			BufferedImage imagen = new BufferedImage(this.getContentPane().getWidth(),this.getContentPane().getHeight(), BufferedImage.TYPE_INT_ARGB);
			Graphics g = imagen.getGraphics();
			this.getContentPane().paint(g);
			ImageIO.write(imagen, formato, Fichero);
			
			
		} catch (IOException ioe) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (CharacterInvalidException cie) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (ZoomRangeException zre) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (DimensionRangeException dre) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (MapTypeException mte) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (NoExistColorException mte) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (SizeRangeException mte) {
			// TODO: handle exception
			Logger.getLogger(this.getClass());
			
		}catch (CoordinateRangeException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
	}
	public static void main(String[] arg){
		Double xLalitude = 7.919593d;
		Double xLonguitude = -72.522397d;
		jMapa aplicacionMapa = new jMapa(xLalitude,xLonguitude);
		//aplicacionMapa.setSize(600, 600);
		aplicacionMapa.setBounds(400,50,600,600);
		aplicacionMapa.setVisible(true);
		
	}

	public void initComponents(){
		txtDire = new JTextField();
		btnBuscar = new JButton("Buscar");
		lblImagenMapa = new JLabel("");
		txtDire.setColumns(20);

		lblImagenMapa.setBorder(BorderFactory.createBevelBorder(0));
		lblImagenMapa.getSize(new java.awt.Dimension(500, 500));
		btnBuscar.addActionListener(this);
		
		panel1 = new Panel();
		
		panel1.add(btnBuscar,BoxLayout.X_AXIS);
		panel1.add(txtDire,BoxLayout.X_AXIS);
		
		panel2 = new Panel();
		panel2.add(lblImagenMapa,BoxLayout.X_AXIS);
		
		myPanel = (JPanel)this.getContentPane();
		
		myPanel.add(panel1,BorderLayout.NORTH);
		myPanel.add(panel2,BorderLayout.CENTER);
	}
}
